Shader "Custom/CheckVertex"
{
    Properties
    {
		[PerRendererData] _MainTex ("Sprite Texture", 2D) = "white" {}
		_Intensity("Intensity", Range(0, 1)) = 1

        _StencilComp ("Stencil Comparison", Float) = 8
        _Stencil ("Stencil ID", Float) = 0
        _StencilOp ("Stencil Operation", Float) = 0
        _StencilWriteMask ("Stencil Write Mask", Float) = 255
        _StencilReadMask ("Stencil Read Mask", Float) = 255

        _ColorMask ("Color Mask", Float) = 15
        [Toggle(UNITY_UI_ALPHACLIP)] _UseUIAlphaClip ("Use Alpha Clip", Float) = 0

		_ClipRect ("_ClipRect",vector) = (0,0,0,0)
    }
    SubShader
    {
		Tags {"Queue" = "Transparent" "RenderType" = "Transparent"}
        LOD 100

		ZWrite Off
		Blend SrcAlpha OneMinusSrcAlpha

        Stencil
        {
            Ref [_Stencil]
            Comp [_StencilComp]
            Pass [_StencilOp] 
            ReadMask [_StencilReadMask]
            WriteMask [_StencilWriteMask]
        }
        ColorMask [_ColorMask]

        Pass
        {
            CGPROGRAM
			#pragma vertex vert
			#pragma fragment frag
			#include "UnityCG.cginc"
            #include "UnityUI.cginc"

            // #### required for Mask ####
			#pragma multi_compile __ UNITY_UI_CLIP_RECT
			float4 _ClipRect;
			#pragma multi_compile __ UNITY_UI_ALPHACLIP

			struct appdata {
				float4 vertex : POSITION;
				float2 uv : TEXCOORD0;
			};

			struct v2f {
				float2 uv : TEXCOORD0;
				float4 vertex : SV_POSITION;

                // #### required for RectMask2D ####
				float4 worldPosition    : TEXCOORD1;
			};

			sampler2D _MainTex;
			float _Intensity;

			v2f vert(appdata v) {
				v2f o;
				o.vertex = UnityObjectToClipPos(v.vertex);
				o.uv = v.uv;
                o.worldPosition = v.vertex;
				return o;
			}

			fixed4 frag(v2f i) : SV_Target {
				fixed4 col = tex2D(_MainTex, i.uv);
				float grayscale = dot(col.rgb, half3(0.2126, 0.7152, 0.0722));
				fixed4 grayColor = fixed4(grayscale, grayscale, grayscale, col.a);

                fixed4 color = lerp(col, grayColor, _Intensity);
                // #### required for RectMask2D ####
				#ifdef UNITY_UI_CLIP_RECT
					color.a *= UnityGet2DClipping(i.worldPosition.xy, _ClipRect);
				#endif

				// #### required for Mask ####
				#ifdef UNITY_UI_ALPHACLIP
					clip (color.a - 0.001);  
				#endif

				return color;
			}
			ENDCG
        }
    }
}
